package com.example.auto_space;

import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CompoundButton;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.Switch;
import android.widget.TextView;

public class EditDo extends Activity {
	
	private RelativeLayout RL;

	private ArrayList<TextView> textviewArray = new ArrayList<TextView>();
	private ArrayList<Switch> switchArray = new ArrayList<Switch>();
	private ArrayList<ListView> listviewArray = new ArrayList<ListView>();
	
	RelativeLayout.LayoutParams lParams = new RelativeLayout.LayoutParams(
	                RelativeLayout.LayoutParams.MATCH_PARENT,
	                RelativeLayout.LayoutParams.MATCH_PARENT);
	
	private int secNum;
	
	//test
	final CharSequence[] roomName = {
			"Room1",
			"LivingRoom",
			"Room2",
			"Toilet"
	};
	final boolean[] onOff = {
			true,
			true,
			false,
			false
	};
	final CharSequence[][] listValue = {
			{"Light", "Computer"},
			{"Light", "TV"},
			{"Laptop"},
			{"Light"}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
//		setContentView(R.layout.activity_edit_do);
		
		RL = new RelativeLayout(this);
		secNum = 0;

		for(int i = 0; i < roomName.length; i++)
		{
			addSection(i, onOff[i]);
		}
        setContentView(RL, lParams);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.edit_do, menu);
		return true;
	}
	
	private void addSection(int sectionNum, boolean switchChecked)
	{
		//id
		//switch: sectionNum*10
		//line  : sectionNum*10+1
		//list	: sectionNum*10+2
		int switchId = sectionNum*10;
		int lineId = switchId+1;
		int listId = switchId+2;
		
		//Create ListView
		ListView list = new ListView(this);
		list.setTag(sectionNum);
		list.setId(listId);
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
		for(int i = 0; i < listValue[sectionNum].length; i++)
		{
			adapter.add(listValue[sectionNum][i].toString());
		}
        list.setAdapter(adapter);
        list.setOnItemClickListener(new AdapterView.OnItemClickListener()
        {
        	@Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id)
            {
                ListView listView = (ListView) parent;
                //get items
                String item = (String) listView.getItemAtPosition(position);
                Log.d("Selected Item", item);

                //jump to Edit Device
                Intent editDeviceIntent = new Intent(getBaseContext(), EditDevice.class);
                editDeviceIntent.putExtra("ITEM_NAME", item);
                startActivity(editDeviceIntent);
            }
        });
        
        //Create Switch
		Switch togSwitch = new Switch(this);
		togSwitch.setTag(sectionNum);
		togSwitch.setId(switchId);
		togSwitch.setChecked(switchChecked);
		if(switchChecked)
			list.setVisibility(View.VISIBLE);
		else
			list.setVisibility(View.GONE);
		togSwitch.setOnCheckedChangeListener(new Switch.OnCheckedChangeListener()
        {
			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				// TODO Auto-generated method stub
				if(isChecked)
					listviewArray.get((Integer) buttonView.getTag()).setVisibility(View.VISIBLE);
				else
					listviewArray.get((Integer) buttonView.getTag()).setVisibility(View.GONE);
			}
        });
		
		//Create Line
		TextView lineView = new TextView(this);
		lineView.setTag(sectionNum);
		lineView.setId(lineId);
		lineView.setBackgroundColor(Color.rgb(220, 220, 220));
		lineView.setGravity(Gravity.CENTER_VERTICAL);
		lineView.setText(" "+roomName[sectionNum]);
		
		//Switch layout
		RelativeLayout.LayoutParams switchRule = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 72);
		if(sectionNum == 0)
			switchRule.addRule(RelativeLayout.ALIGN_PARENT_TOP);
		else
			switchRule.addRule(RelativeLayout.BELOW, listviewArray.get(sectionNum-1).getId());
		togSwitch.setLayoutParams(switchRule);
		//Line layout
		RelativeLayout.LayoutParams lineRule = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 72);
		lineRule.addRule(RelativeLayout.ALIGN_BOTTOM, switchId);
		lineView.setLayoutParams(lineRule);
		//Listview layout
		RelativeLayout.LayoutParams listRule = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
		listRule.addRule(RelativeLayout.BELOW, lineId);
		list.setLayoutParams(listRule);

		RL.addView(lineView, lineRule);
		RL.addView(togSwitch, switchRule);
		RL.addView(list, listRule);
		
        listviewArray.add(list);
		switchArray.add(togSwitch);
		textviewArray.add(lineView);
		
		secNum++;
	}
}
